/* Hawk - A Component Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.applications.finder.swing.gui;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * User interface used to create the profile
 * 
 * @author Rodrigo Prestes Machado
 */
@SuppressWarnings("serial")
public class TabProfile extends JPanel {

	/* Attributes */
	private static byte profile[];
	
	private static final Integer[] COMBO_VALUES = {1,2,3,4,5};  
	
	private GridBagLayout layout;
	private GridBagConstraints constraints;
	
	private JComboBox comboTeste;
	private JComboBox comboHardware;
	private JComboBox comboModel;
	private JComboBox comboNetwork;
	private JComboBox comboProject;
	
	public TabProfile(){

		// creating the user
		profile = new byte[5];
	
		// creating layout 
		layout = new GridBagLayout();
		setLayout(layout);
		
		// configuring layout
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(5,5,5,5);
		
		// creating interfaces elements
		JLabel labelTeste = new JLabel("Test");
		comboTeste = new JComboBox(COMBO_VALUES);
		
		JLabel labelHardware = new JLabel("Hardware");
		comboHardware = new JComboBox(COMBO_VALUES);
		
		JLabel labelModel = new JLabel("Model");
		comboModel = new JComboBox(COMBO_VALUES);
		
		JLabel labelNetwork = new JLabel("Network");
		comboNetwork = new JComboBox(COMBO_VALUES);
		
		JLabel labelProject = new JLabel("Project");
		comboProject = new JComboBox(COMBO_VALUES);
		
		JButton botao = new JButton("Save Profile");
		botao.addActionListener(new EventSaveProfile());
		
		// adding the gui components on screen
		addComponent(labelTeste, 1, 1, 1, 1);
		addComponent(comboTeste, 1, 2, 1, 1);
		
		addComponent(labelHardware, 2, 1, 1, 1);
		addComponent(comboHardware, 2, 2, 1, 1);
		
		addComponent(labelModel, 3, 1, 1, 1);
		addComponent(comboModel, 3, 2, 1, 1);
		
		addComponent(labelNetwork, 4, 1, 1, 1);
		addComponent(comboNetwork, 4, 2, 1, 1);
		
		addComponent(labelProject, 5, 1, 1, 1);
		addComponent(comboProject, 5, 2, 1, 1);
		
		addComponent(botao, 6, 1, 1, 1);
	}
	
	/**
	 * Description : Put a swing component on panel
	 * 
	 * @param Componente component : One gui component
	 * @param int row    : The row number of the component (position on screen)
	 * @param int column : The row column of the component (position on screen)
	 * @param int width  : Number of column that the component occupies
	 * @param int height : Number of rows that the component occupies
	 */
	private void addComponent(Component component, int row, int column, int width, int height) {
		
		// row number
		constraints.gridy = row;
		
		// column number
		constraints.gridx = column;
		
		// number of column that the component occupies
		constraints.gridwidth = width;
		
		// number of rows that the component occupies
		constraints.gridheight = height;

		layout.setConstraints(component, constraints);
		add(component);
	}
	
	/**
	 *  Inner Class to handle the save button event and store the user profile
	 */
	private class EventSaveProfile implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			
			// getting the values of user profile
			profile[0] = ((Integer) comboTeste.getSelectedItem()).byteValue();
			profile[1] = ((Integer) comboHardware.getSelectedItem()).byteValue();
			profile[2] = ((Integer) comboModel.getSelectedItem()).byteValue();
			profile[3] = ((Integer) comboNetwork.getSelectedItem()).byteValue();
			profile[4] = ((Integer) comboProject.getSelectedItem()).byteValue();
			
			JOptionPane.showMessageDialog(null, "Saved");	
		}
	}

	/**
	 * Returns the user profile
	 * 
	 * @return an array of bytes
	 */
	public static byte[] getProfile() {
		return profile;
	}
}